#!/bin/sh

SHELL_FOLDER=$(cd $(dirname ${BASH_SOURCE:-$0});pwd)
PROJECT_DIR=$(pwd)

PLATFORM=$1

source $SHELL_FOLDER/util.sh

# 更新配置
sh $SHELL_FOLDER/import.sh

# 清空缓存
flutter clean

# 编译
flutter build ios --release

DIST_DIR=dist/ios
PAYLOAD_PATH=$DIST_DIR/Payload
TIME_S=`date +%s`
IPA_NAME=Runner_$TIME_S.ipa

rm -rf $PAYLOAD_PATH
mkdir -p $PAYLOAD_PATH

APP_PATH=$PROJECT_DIR/build/ios/iphoneos/Runner.app

cp -r $APP_PATH "$PAYLOAD_PATH/"


# SwiftSupport
cd $DIST_DIR
rm -rf Payload/.DS_Store
if [[ $PLATFORM == 'APPSTORE' ]]; then
createSwiftSupport "Payload/Runner.app/Frameworks"
zip -r $IPA_NAME Payload SwiftSupport
rm -rf Payload
rm -rf SwiftSupport
else
zip -r $IPA_NAME Payload
rm -rf Payload
fi
cd $PROJECT_DIR
echo "打包成功：$PROJECT_DIR/$DIST_DIR/$IPA_NAME"

# 这里可以写上传到对应的服务端（例如蒲公英、App Store等）之类的功能
# https://help.apple.com/asc/appsaltool/#/apdATD1E53-D1E1A1303-D1E53A1126
# API_KEY=
# ISSUSER_ID=
# xcrun altool --upload-app --type ios -f "$DIST_DIR/$IPA_NAME" --apiKey $API_KEY --apiIssuer $ISSUSER_ID

